package com.origin.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.ParcelFileDescriptor;

import androidx.annotation.RequiresApi;

import java.io.FileDescriptor;
import java.io.IOException;

public class WallpaperUtils {

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void  setLockWallPaper(Context mContext , String imagePath){

        try {
            WallpaperManager mWallManager = WallpaperManager.getInstance(mContext);
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
            mWallManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void  setLockWallPaper(Context mContext , Bitmap bitmap){

        try {
            WallpaperManager mWallManager = WallpaperManager.getInstance(mContext);
            mWallManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //使用WallpaperManager类
    @SuppressLint("MissingPermission")
    @TargetApi(Build.VERSION_CODES.N)
    public static Bitmap getLockWallpaper(Context mContext){
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);//获取WallpaperManager实例
        @SuppressLint("MissingPermission") ParcelFileDescriptor mParcelFileDescriptor = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);//获取锁屏壁纸
        if(mParcelFileDescriptor ==null){
            mParcelFileDescriptor = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);//获取桌面壁纸
            //在未主动设置锁屏壁纸时，锁屏壁纸为null，默认取的是桌面壁纸，
        }
        FileDescriptor fileDescriptor = mParcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);//获取Bitmap类型返回值
        try {
            mParcelFileDescriptor.close();
        } catch(Exception e) {
        }
        return image;
    }


}
